/* 
 * Copyright (C) 2011, WordCards4Me Project - http://code.google.com/p/wordcards4me/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.wordcards4me;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;

public final class CardCanvas extends Canvas {

	public static final int PORTRAIT = Sprite.TRANS_NONE;
	public static final int LANDSCAPE = Sprite.TRANS_ROT90;
	
	// Keys
	private static final int PREVIOUS_CARD_CODE =   -1;
	private static final int NEXT_CARD_CODE =       -2;
	private static final int SHOW_TRANSLATE_CODE =  -5;
	private static final int EXIT_CODE = 53;

	private static CardCanvas instance;
	
	private Font font;
	
	private int fontColor;
	private int canvasBackgroundColor;
	private int cardBackgroundColor;
	
	private int orientation;

	private boolean isShowTranslate;

	private Sequence sequence;
	
	public static CardCanvas get() {
		if(instance == null) {
			instance = new CardCanvas();
		}
		return instance;
	}
	
	public void setStack(Sequence stack) {
		this.sequence = stack;
	}
	
	private CardCanvas() {
		setFullScreenMode(true);
	}
	
	public void updateSettings() {
		loadAndApplyProperties();
	}

	private void loadAndApplyProperties() {
		font = Font.getFont(
				CardMidlet.getInteger("wc4me.font.face"),
				CardMidlet.getInteger("wc4me.font.style"),
				CardMidlet.getInteger("wc4me.font.size"));
		canvasBackgroundColor = CardMidlet.getInteger("wc4me.color.bkgcanvas");
		fontColor = CardMidlet.getInteger("wc4me.color.font");
		cardBackgroundColor = CardMidlet.getInteger("wc4me.color.bkgcard");
		orientation = CardMidlet.getInteger("wc4me.canvas.orientation");
	}
	
	protected void paint(Graphics g) {
		if(sequence == null) {
			g.setColor(0x000000);
			g.fillRect(0, 0, getWidth(), getHeight());
			return;
		}
		Image bufferImage = Image.createImage(getHeight(), getWidth());
		Graphics bufferGraphics = bufferImage.getGraphics();

		drawBackground(bufferGraphics, bufferImage);

		if (isShowTranslate) {
			drawPhraseWithDescription(bufferGraphics, bufferImage);
		} else {
			drawPhrase(bufferGraphics, bufferImage);
		}
		g.drawRegion(bufferImage, 0, 0, bufferImage.getWidth(),
				bufferImage.getHeight(), Sprite.TRANS_ROT90, 0, 0,
				Graphics.LEFT | Graphics.TOP);
	}

	private void drawPhraseWithDescription(Graphics bGraphics, Image bImage) {
		final int oldColor = bGraphics.getColor();
		final Font oldFont = bGraphics.getFont();

		bGraphics.setColor(fontColor);
		bGraphics.setFont(font);
		
		bGraphics.drawString(
				sequence.currentCard().getPhrase().toUpperCase(),
				bImage.getWidth() / 2, (bImage.getHeight() / 2) - 20,
				Graphics.HCENTER | Graphics.BASELINE);
		bGraphics.drawString(
				sequence.currentCard().getTransl().toUpperCase(),
				bImage.getWidth() / 2, (bImage.getHeight() / 2) + 20,
				Graphics.HCENTER | Graphics.BASELINE);

		bGraphics.setColor(oldColor);
		bGraphics.setFont(oldFont);
	}

	
	private void drawPhrase(Graphics g, Image buffer) {
		final int oldColor = g.getColor();
		final Font oldFont = g.getFont();

		g.setColor(fontColor);
		g.setFont(font);
		g.drawString(sequence.currentCard().getPhrase().toUpperCase(),
				buffer.getWidth() / 2, buffer.getHeight() / 2, Graphics.HCENTER
						| Graphics.BASELINE);

		g.setColor(oldColor);
		g.setFont(oldFont);
	}

	private void drawBackground(Graphics g, Image buffer) {
		int previousColor = g.getColor();
		g.setColor(canvasBackgroundColor);
		g.fillRect(0, 0, buffer.getWidth(), buffer.getHeight());
		g.setColor(cardBackgroundColor);
		g.fillRoundRect(3, 3, buffer.getWidth() - 7, buffer.getHeight() - 7,
				10, 10);
		g.setColor(previousColor);
	}

	protected void keyPressed(int keyCode) {
		switch (keyCode) {
		case PREVIOUS_CARD_CODE:
			sequence.previousCard();
			isShowTranslate = false;
			repaint();
			break;
		case NEXT_CARD_CODE:
			sequence.nextCard();
			isShowTranslate = false;
			repaint();
			break;
		case SHOW_TRANSLATE_CODE:
			isShowTranslate = !isShowTranslate;
			repaint();
			break;
		case EXIT_CODE:
			sequence = null;
			repaint();
			CardMidlet.goBackward();
			break;
		default:
			// do nothing
		}
	}
}
